package com.chixing.day20_annotation_jdk8.work;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CoutomerDemo {
    public static void main(String[] args) {
        Coutomer coutomer1 = new Coutomer("000001","刘德华","0000",1,4155990188888888L, LocalDateTime.of(2006,07,20,20,00,05),300.00);
        Coutomer coutomer2 = new Coutomer("000201","晓龙","0002",1,4155990199999999L,LocalDateTime.of(2005,07,20,20,00,05),500.00);
        Coutomer coutomer3 = new Coutomer("000101","黄晓明","0012",1,4155990100000000L,LocalDateTime.of(2007,07,20,20,00,05),1000.50);
        Coutomer coutomer4 = new Coutomer("000101","张东健","0012",1,4155990155555555L,LocalDateTime.of(2006,07,20,20,00,05),600.99);
        Coutomer coutomer5 = new Coutomer("000301","梁朝伟","0013",0,4155990111111111L,LocalDateTime.of(2005,07,22,20,10,05),5000.00);
        Coutomer coutomer6 = new Coutomer("000001","刘德华","0000",1,4155990188888888L,LocalDateTime.of(2004,07,25,20,00,05),200.00);

        List<Coutomer> arrayList = new ArrayList<>();
        arrayList.add(coutomer1);
        arrayList.add(coutomer2);
        arrayList.add(coutomer3);
        arrayList.add(coutomer4);
        arrayList.add(coutomer5);
        arrayList.add(coutomer6);

        findByCustomerNumber(arrayList,coutomer2.getCouId()).forEach(System.out::println);
        findByDate(arrayList,"2004-07-25 20:00:05","2006-07-20 20:00:05").forEach(System.out::println);
        System.out.println(totalAmount(arrayList));

    }

    /**
     * 按客户号查询记录
     *
     * @param customerNumber - 客户号
     * @return 符合条件的记录集合或null
     */
    public static List<Coutomer> findByCustomerNumber(List<Coutomer> arrayList,String customerNumber) {
        return arrayList.stream().filter(coutomer -> coutomer.getCouId().equals(customerNumber)).collect(Collectors.toList());
    }

    /**
     * 按日期段查询记录
     *
     * @param startDate - 开始日期
     * @param endDate   - 结束日期
     * @return 符合条件的记录数组或null
     */
    public static List<Coutomer> findByDate(List<Coutomer> arrayList,String startDate, String endDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return arrayList.stream().filter(coutomer -> coutomer.getCouTime().isAfter(LocalDateTime.parse(startDate,formatter))&&
                        coutomer.getCouTime().isBefore(LocalDateTime.parse(endDate,formatter))).collect(Collectors.toList());
    }

    /**
     * 取得总金额
     * @return 总金额
     */
    public static double totalAmount(List<Coutomer> arrayList) {
        double n = arrayList.stream().map((cost) -> cost.getCouMoney()).reduce((sum,cost)->sum +cost).get();
        return n;
    }
}
